.text
.section .rodata
.align 3
.LC1:
        .string " YEAR RAINFALL (inches)"
        .align  3
.LC2:
        .string "%5d %15.1f\n"
        .align  3
.LC4:
        .string "\nThe yearly average is %.1f inches.\n\n"
        .align  3
.LC5:
        .string "MONTHLY AVERAGES:\n"
        .align  3
.LC6:
        .string " Jan Feb Mar Apr May Jun Jul Aug Sep Oct "
        .align  3
.LC7:
        .string " Nov Dec"
        .align  3
.LC8:
        .string "%4.1f "
        .align  3
.LC0:
        .word   1082759578
        .word   1082759578
        .word   1082759578
        .word   1077936128
        .word   1073741824
        .word   1067030938
        .word   1045220557
        .word   1045220557
        .word   1053609165
        .word   1075419546
        .word   1080033280
        .word   1087583027
        .word   1091043328
        .word   1090728755
        .word   1067030938
        .word   1070386381
        .word   1075419546
        .word   0
        .word   1084647014
		.word   1063675494
        .word   1050253722
        .word   1063675494
        .word   1068708659
        .word   1089051034
        .word   1091672474
        .word   1091043328
        .word   1087792742
        .word   1082759578
        .word   1074161254
        .word   1061997773
        .word   1045220557
        .word   1045220557
        .word   1066192077
        .word   1075000115
        .word   1086534451
        .word   1090938470
        .word   1088841318
        .word   1092511334
        .word   1090938470
        .word   1079194419
        .word   1067030938
        .word   1061997773
        .word   1053609165
        .word   0
        .word   1058642330
        .word   1071225242
        .word   1082759578
        .word   1086744166
        .word   1089680179
        .word   1085485875
        .word   1081291571
        .word   1077097267
        .word   1081291571
        .word   1045220557
        .word   0
        .word   0
        .word   0
        .word   1067869798
        .word   1076258406
        .word   1084647014

.LC3:
         .word   1084227584

.text
.globl main
.type main, @function
main:
	addi sp, sp, -272
	sd ra, 264(sp)
	sd s0, 256(sp)
	addi s0, sp, 256

	// 这里初始化过程比x86的简单多了
	lui a5, %hi(.LC0)
	addi a1, a5, %lo(.LC0)
	addi a0, s0, -256
	li a5, 240
	mv a2, a5
	call memcpy

	lui a5, %hi(.LC1)
	addi a0, a5, %lo(.LC1)
	call puts

	// 根据x86的代码，把各变量的位置规划一下，这就是把s0设置为返回地址和栈基址下面的好处，跟x86就一样的布局了。
	// -16(s0) /* year */
	// -12(s0) /* total */
	// -8(s0) /* month */
	// -4(s0) /* subtot */

	sw zero, -16(s0) /* year */
	sw zero, -12(s0) /* total */
	j .L2
.L1:
	sw zero, -8(s0) /* month */
	sw zero, -4(s0) /* subtot */
	j .L4
.L3:
	lw a5, -16(s0) /* year */
	sext.w a5, a5
	li a4, 48
	mul a5, a4, a5
	add a5, a5, s0
	addi a5, a5, -256
	mv a4, a5

	lw a5, -8(s0) /* month */
	slli a5, a5, 2
	add a5, a4, a5

	flw fa5, (a5)
	flw fa4, -4(s0) /* subtot */
	fadd.s fa5, fa4, fa5
	fsw fa5, -4(s0) /* subtot */

	// month++
	lw a5, -8(s0) /* month */
	addiw a5, a5, 1
	sw a5, -8(s0) /* month */
.L4:
	lw a5, -8(s0) /* month */
	sext.w a5, a5
	li a4, 12
	blt a5, a4, .L3

	lw a5, -16(s0) /* year */
	addiw a1, a5, 2010
	flw fa5, -4(s0) /* subtot */
	fcvt.d.s fa5, fa5
	fmv.x.d a2, fa5
	lui a5, %hi(.LC2)
	addi a0, a5, %lo(.LC2)
	call printf

	// total += subtot
	flw fa5, -12(s0) /* total */
	flw fa4, -4(s0) /* subtot */
	fadd.s fa5, fa4, fa5
	fsw fa5, -12(s0) /* total */

	// year++
	lw a5, -16(s0) /* year */
	addiw a5, a5, 1
	sw a5, -16(s0) /* year */
.L2:
	lw a5, -16(s0) /* year */
	sext.w a5, a5
	li a4, 5
	blt a5, a4, .L1

	flw fa5, -12(s0) /* total */
	lui a5, %hi(.LC3)
	flw fa4, %lo(.LC3)(a5)
	fdiv.s fa5, fa5, fa4
	fcvt.d.s fa5, fa5
	fmv.x.d a1, fa5
	lui     a5,%hi(.LC4)
    addi    a0,a5,%lo(.LC4)
    call    printf

	lui     a5,%hi(.LC5)
	addi    a0,a5,%lo(.LC5)
	call    puts
	lui     a5,%hi(.LC6)
	addi    a0,a5,%lo(.LC6)
	call    printf
	lui     a5,%hi(.LC7)
	addi    a0,a5,%lo(.LC7)
	call puts

	sw zero, -8(s0) /* month */
	j .L6
.L5:
	sw zero, -16(s0) /* year */
	sw zero, -4(s0) /* subtot */
	j .L8
.L7:
	lw a5, -16(s0) /* year */
	sext.w a5, a5
	li a4, 48
	mul a5, a4, a5
	add a5, a5, s0
	addi a5, a5, -256
	mv a4, a5

	lw a5, -8(s0) /* month */
	slli a5, a5, 2
	add a5, a4, a5

	flw fa5, (a5)
	flw fa4, -4(s0) /* subtot */
	fadd.s fa5, fa4, fa5
	fsw fa5, -4(s0) /* subtot */

	lw a5, -16(s0) /* year */
	addiw a5, a5, 1
	sw a5, -16(s0) /* year */
.L8:
	lw a5, -16(s0) /* year */
	sext.w a5, a5
	li a4, 5
	blt a5, a4, .L7

	flw fa5, -4(s0) /* subtot */
	lui a5, %hi(.LC3)
	flw fa4, %lo(.LC3)(a5)
	fdiv.s fa5, fa5, fa4
	fcvt.d.s fa5, fa5
	fmv.x.d a1, fa5
	lui a5, %hi(.LC8)
	addi a0, a5, %lo(.LC8)
	call printf

	lw a5, -8(s0) /* month */
	addiw a5, a5, 1
	sw a5, -8(s0) /* month */
.L6:
	lw a5, -8(s0) /* month */
	sext.w a5, a5
	li a4, 12
	blt a5, a4, .L5

	li a5, 10
	mv a0, a5
	call putchar

	ld ra, 264(sp)
	ld s0, 256(sp)
	addi sp, sp, 272
	jr ra